package com.vehicle.car.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.vehicle.car.perpetual.MyPerpetual;
import com.vehicle.car.utils.HttpUtils;
import com.vehicle.car.utils.ImageToBase64Utils;
import com.vehicle.car.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vehicle.car.mapper.DriverLicenseInfoMapper;
import com.vehicle.car.domain.DriverLicenseInfo;
import com.vehicle.car.service.IDriverLicenseInfoService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 驾驶证Service业务层处理
 *
 * @author XU
 * @date 2025-03-30
 */
@Service
public class DriverLicenseInfoServiceImpl implements IDriverLicenseInfoService
{
    @Autowired
    private DriverLicenseInfoMapper driverLicenseInfoMapper;

    /**
     * 查询驾驶证
     *
     * @param id 驾驶证主键
     * @return 驾驶证
     */
    @Override
    public DriverLicenseInfo selectDriverLicenseInfoById(Long id)
    {
        return driverLicenseInfoMapper.selectDriverLicenseInfoById(id);
    }

    /**
     * 查询驾驶证列表
     *
     * @param driverLicenseInfo 驾驶证
     * @return 驾驶证
     */
    @Override
    public List<DriverLicenseInfo> selectDriverLicenseInfoList(DriverLicenseInfo driverLicenseInfo)
    {
        return driverLicenseInfoMapper.selectDriverLicenseInfoList(driverLicenseInfo);
    }

    /**
     * 新增驾驶证
     *
     * @param driverLicenseInfo 驾驶证
     * @return 结果
     */
    @Override
    public int insertDriverLicenseInfo(DriverLicenseInfo driverLicenseInfo)
    {
        return driverLicenseInfoMapper.insertDriverLicenseInfo(driverLicenseInfo);
    }

    /**
     * 修改驾驶证
     *
     * @param driverLicenseInfo 驾驶证
     * @return 结果
     */
    @Override
    public int updateDriverLicenseInfo(DriverLicenseInfo driverLicenseInfo)
    {
        return driverLicenseInfoMapper.updateDriverLicenseInfo(driverLicenseInfo);
    }

    /**
     * 批量删除驾驶证
     *
     * @param ids 需要删除的驾驶证主键
     * @return 结果
     */
    @Override
    public int deleteDriverLicenseInfoByIds(Long[] ids)
    {
        return driverLicenseInfoMapper.deleteDriverLicenseInfoByIds(ids);
    }

    /**
     * 删除驾驶证信息
     *
     * @param id 驾驶证主键
     * @return 结果
     */
    @Override
    public int deleteDriverLicenseInfoById(Long id)
    {
        return driverLicenseInfoMapper.deleteDriverLicenseInfoById(id);
    }

    @Override
    public JSONObject drivingLicence(MultipartFile file) {
        try {
            String timestamp = System.currentTimeMillis() + "";
            String image = ImageToBase64Utils.imageToBase64(file);
            String sign = MD5Utils.encrypt(MyPerpetual.APP_ID + "&" + timestamp + "&" + MyPerpetual.APPSECURITY);

            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/x-www-form-urlencoded");

            Map<String, Object> params = new HashMap<>();
            params.put("appid", MyPerpetual.APP_ID);
            params.put("timestamp", timestamp);
            params.put("sign", sign);
            params.put("image", image);

            String result1 = HttpUtils.postForm(MyPerpetual.DRIVING_URL, headers, params);
            JSONObject jsonObject = JSONObject.parseObject(result1);
            System.out.println("OCR返回结果：" + jsonObject.toJSONString());

            if (jsonObject != null && jsonObject.getInteger("code") == 200) {
                JSONObject data = jsonObject.getJSONObject("data");
                if (data != null) {
                    JSONObject result = data.getJSONObject("result");
                    if (result != null) {
                        JSONObject pageA = result.getJSONObject("page_a");
                        if (pageA != null) {
                            // 构建驾驶证信息对象
                            DriverLicenseInfo driverLicenseInfo = new DriverLicenseInfo();
                            
                            // 设置必填字段
                            driverLicenseInfo.setFileNumber(pageA.getString("file_number"));
                            driverLicenseInfo.setName(pageA.getString("name"));
                            driverLicenseInfo.setDriverId(pageA.getString("id"));
                            driverLicenseInfo.setBirthday(pageA.getString("birthday"));
                            driverLicenseInfo.setAddress(pageA.getString("address"));
                            driverLicenseInfo.setGender(pageA.getString("gender"));
                            driverLicenseInfo.setValidFrom(pageA.getString("valid_from"));
                            driverLicenseInfo.setType(pageA.getString("type"));
                            driverLicenseInfo.setVersion(pageA.getString("version"));
                            driverLicenseInfo.setDateOfFirstIssue(pageA.getString("date_of_first_issue"));
                            driverLicenseInfo.setValidDate(pageA.getString("valid_date"));
                            driverLicenseInfo.setNationality(pageA.getString("nationality"));
                            driverLicenseInfo.setIssueBy(pageA.getString("issue_by"));
                            driverLicenseInfo.setValidFor(pageA.getString("valid_for"));

                            try {
                                // 保存到数据库并检查结果
                                int rows = driverLicenseInfoMapper.insertDriverLicenseInfo(driverLicenseInfo);
                                System.out.println("数据库插入结果：" + rows);
                                if (rows <= 0) {
                                    System.out.println("数据库插入失败");
                                }
                            } catch (Exception e) {
                                System.out.println("数据库操作异常：" + e.getMessage());
                                e.printStackTrace();
                            }
                        } else {
                            System.out.println("page_a 节点为空");
                        }
                    }
                }
            }
            return jsonObject;
        } catch (Exception e) {
            System.out.println("处理异常：" + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

}
